//
//  LoginAPI.swift
//  kano
//
//  Created by Sophia on 2024/8/16.
//

import Foundation
import Moya
import KakaJSON

enum LoginAPI {
    case getConfig
    case checkService(_ parameters: Dictionary<String, Any>? = nil)
    case autoLogin(_ parameters: Dictionary<String, Any>? = nil)
    case checkMobile(_ parameters: Dictionary<String, Any>? = nil)
    case sendCode(_ parameters: Dictionary<String, Any>? = nil)
    case checkSmsCode(_ parameters: Dictionary<String, Any>? = nil)
    case registerPwd(_ parameters: Dictionary<String, Any>? = nil)
    case login(_ parameters: Dictionary<String, Any>? = nil)
    case checkClientId(_ parameters: Dictionary<String, Any>? = nil)
    
    case getApply(_ parameters: Dictionary<String, Any>? = nil)
    
    case logOff(_ parameters: Dictionary<String, Any>? = nil)
    
    case picVerify(_ parameters: Dictionary<String, Any>? = nil)
    
    
    case creditStatus(_ parameters: Dictionary<String, Any>? = nil)
    case homeInfo(_ parameters: Dictionary<String, Any>? = nil)
    case loanBillList(_ parameters: Dictionary<String, Any>? = nil)
    case userAFInfo(_ parameters: Dictionary<String, Any>? = nil)
    
    case changePwdVerCode(_ parameters: Dictionary<String, Any>? = nil)
    
    case changePwd(_ parameters: Dictionary<String, Any>? = nil)
    case verifyOldPwd(_ parameters: Dictionary<String, Any>? = nil)
    case modifyOldPwd(_ parameters: Dictionary<String, Any>? = nil)
    
    case validateMobile(_ parameters: Dictionary<String, Any>? = nil)
    case validateLicense(_ parameters: Dictionary<String, Any>? = nil)
    case validateNewMobileCode(_ parameters: Dictionary<String, Any>? = nil)
    case modifyMobile(_ parameters: Dictionary<String, Any>? = nil)
    
    case homeNews(_ parameters: Dictionary<String, Any>? = nil)
    case homeCoupon(_ parameters: Dictionary<String, Any>? = nil)
    case homeBanner(_ parameters: Dictionary<String, Any>? = nil)
    case checkPictureCode(_ parameters: Dictionary<String, Any>? = nil)
    case checkUserType(_ parameters: Dictionary<String, Any>? = nil)
    case queryCreditResult(_ parameters: Dictionary<String, Any>? = nil)
    case getExitReason(_ parameters: Dictionary<String, Any>? = nil)
    case saveLeaveReason(_ parameters: Dictionary<String, Any>? = nil)
    
}

extension LoginAPI : TargetType {
    var baseURL: URL {
        switch self {
        case .getConfig:
            return URL(string: kConfigUrl)!
        default:
            return URL(string: NetConfig.readBaseUrl())!
        }
    }
    
    var path: String {
        switch self {
            //Post
        case .checkService:
            let path = NetConfig.fullNormal(path: "/available")
            return path
        case .checkMobile:
            let path = NetConfig.fullNormal(path: "/userProfileUpdate")
            return path
        case .sendCode:
            let path = NetConfig.fullNormal(path: "/smsAuthCodeRetrieve")
            return path
        case .checkSmsCode:
            let path = NetConfig.fullNormal(path: "/smsCodeVerification")
            return path
        case .registerPwd:
            let path = NetConfig.fullNormal(path: "/signupProcessInitiate")
            return path
        case .login:
            let path = NetConfig.fullNormal(path: "/standardUserLogin")
            return path
        case .checkClientId:
            let path = NetConfig.fullNormal(path: "/checkClientId")
            return path
    
        case .logOff:
            let path = NetConfig.fullNormal(path: "/verifycodelogoff")
            return path
        case .picVerify:
            let path = NetConfig.fullNormal(path: "/generateUserAvatar")
            return path
        case .autoLogin:
            let path = NetConfig.fullNormal(path: "/autoDeviceLoginSetup")
            return path
  
        case .homeInfo:
            let path = NetConfig.fullNormal(path: "/mainHomepageView")
            return path
        case .creditStatus:
            let path = NetConfig.fullNormal(path: "/creditStatusInquiry")
            return path
        case .loanBillList:
            let path = NetConfig.fullNormal(path: "/overdueFeePolicy")
            return path
            
        case .userAFInfo:
//            let path = NetConfig.fullNormal(path: "/user_apps_data_repository")
            let path = NetConfig.fullNormal(path: "/user_apps_data_repository_v2")

            return path
            
        case .changePwdVerCode:
            let path = NetConfig.fullNormal(path: "/verifyTradeCode")
            return path
            
        case .changePwd:
            let path = NetConfig.fullNormal(path: "/setSMSPass")
            return path
            
        case .verifyOldPwd:
            let path = NetConfig.fullNormal(path: "/verifyOldPassword")
            return path
           
        case .modifyOldPwd:
            let path = NetConfig.fullNormal(path: "/modifyUserPassword")
            return path
            
        case .validateMobile:
            let path = NetConfig.fullNormal(path: "/validateUser")
            return path
            
        case .validateLicense:
            let path = NetConfig.fullNormal(path: "/validateLicense")
            return path
            
        case .validateNewMobileCode:
            let path = NetConfig.fullNormal(path: "/verifyCodepath")
            return path
           
        case .modifyMobile:
            let path = NetConfig.fullNormal(path: "/completeVerify")
            return path
           
        case .homeNews:
            let path = NetConfig.fullNormal(path: "/breakingNewsFeed")
            return path
            
        case .homeCoupon:
            let path = NetConfig.fullNormal(path: "/getPopupVoucher")
            return path
            
        case .homeBanner:
            let path = NetConfig.fullNormal(path: "/featuredIndex")
            return path
            
        case .checkPictureCode:
            let path = NetConfig.fullNormal(path: "/validateUserAvatar")
            return path
            
        case .checkUserType:
            let path = NetConfig.fullNormal(path: "/home_green")
            return path
            
        case .queryCreditResult:
            let path = NetConfig.fullNormal(path: "/home_credit_result")
            return path
            
        case .getExitReason:
            let path = NetConfig.fullNormal(path: "/getAuthConfig")
            return path
            
        case .saveLeaveReason:
            let path = NetConfig.fullNormal(path: "/saveUserConfig")
            return path
            
            //Get
        case .getApply(let parameters):
            let path = NetConfig.fullApply(path: "", parameters: parameters)
            return path
        default:
            return ""
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getConfig: fallthrough
        case .getApply:
            return .get
        default:
            return .post
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .checkService(let parameters): fallthrough
        case .creditStatus(let parameters): fallthrough
        case .homeInfo(let parameters): fallthrough
        case .loanBillList(let parameters): fallthrough
        case .checkMobile(let parameters): fallthrough
        case .sendCode(let parameters): fallthrough
        case .checkSmsCode(let parameters): fallthrough
        case .registerPwd(let parameters): fallthrough
        case .checkClientId(let parameters): fallthrough
        case .logOff(let parameters): fallthrough
        case .autoLogin(let parameters): fallthrough
        case .userAFInfo(let parameters): fallthrough
        case .changePwdVerCode(let parameters): fallthrough
        case .changePwd(let parameters): fallthrough
        case .verifyOldPwd(let parameters): fallthrough
        case .modifyOldPwd(let parameters): fallthrough
        case .validateMobile(let parameters): fallthrough
        case .validateLicense(let parameters): fallthrough
        case .validateNewMobileCode(let parameters): fallthrough
        case .modifyMobile(let parameters): fallthrough
        case .homeNews(let parameters): fallthrough
        case .homeCoupon(let parameters): fallthrough
        case .homeBanner(let parameters): fallthrough
        case .checkPictureCode(let parameters): fallthrough
        case .checkUserType(let parameters): fallthrough
        case .queryCreditResult(let parameters): fallthrough
        case .getExitReason(let parameters): fallthrough
        case .saveLeaveReason(let parameters): fallthrough
            
        case .login(let parameters):
            if kisEncrypt == 1 {
                let data: Dictionary<String, Any>
                if (parameters == nil) {
                    data = ["body" : ""]
                } else {
                    let bodyData = KAEncrypt.ka_encrypt(parameters!.jsonString()!)
                    data = ["body" : bodyData]
                }
                return .requestCompositeParameters(bodyParameters: data as [String : Any], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            } else {
                return .requestCompositeParameters(bodyParameters:parameters ?? ["" : ""], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            }
        case .picVerify(_):
            return .requestParameters(parameters: ["body" : ""] as [String : Any], encoding: JSONEncoding.default)
        case .getApply(let parameters):
            return .requestParameters(parameters: parameters!, encoding: URLEncoding.default)
        default:
            return .requestPlain
        }
    }
    
    var headers: [String : String]? {
        switch self {
        case .getConfig:
            return ["Content-Type":"application/json; charset=utf-8"]
        default:
            return ["Content-Type":"application/json; charset=utf-8",
                    "SW-K-ID": UIDevice.ka_UUID.toBase64()]
        }
    }
}




